﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineVideos.Services.FileStoreService;
using System.Windows;
using System.ServiceModel;
using System.IO.IsolatedStorage;
using System.IO;

namespace Common.Services
{
    class ServiceRestoreData
    {
        public static int restoreFileCount = 0;
        public static int restoreFileCompletedCount = 0;
        public static int restoreFileNotCompletedCount = 0;
        public static string Id = "";

         #if(Usedinskydriverestoring)
        public static void RestoreData(string UserId)
        {
            Id = UserId;
            Service1Client service = new Service1Client();
            service.GetUserFileNamesAsync("activitytracker", UserId, "userdata");
            service.GetUserFileNamesCompleted += new EventHandler<GetUserFileNamesCompletedEventArgs>(service_GetUserFileNamesCompleted);
        }
        static void service_GetUserFileNamesCompleted(object sender, GetUserFileNamesCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    if (e.Result != null && e.Result.Count > 0)
                    {
                        restoreFileCount = e.Result.Count;
                        foreach (string file in e.Result)
                        {
                            string isofilename = "";
                            Service1Client service1 = new Service1Client();
                            service1.RestoreUserDataAsync("activitytracker", Id, "userdata", file, isofilename);
                            service1.RestoreUserDataCompleted += new EventHandler<RestoreUserDataCompletedEventArgs>(service1_RestoreUserDataCompleted);
                        }
                    }
                    else
                    {
                        MessageBox.Show("There is no data for the specified User or Data doesn't exist");
                    }
                }
                else if (e.Error is FaultException)
                {

                    MessageBox.Show("Failed to restore data, Please try again later");
                    FaultException fault = e.Error as FaultException;
                    //Exception.SaveExceptionInLocalStorage(e.Error.Message);
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in service_GetUserFileNamesCompleted Method In Settings file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                //  Exceptions.SaveOrSendExceptions(mess, ex.Data);
            }
        }

        static void service1_RestoreUserDataCompleted(object sender, RestoreUserDataCompletedEventArgs e)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream isostream;
            try
            {
                if (e.Error == null)
                    restoreFileCompletedCount++;
                else
                    restoreFileNotCompletedCount++;

                if (e.Result != null)
                {
                    if (!isoStore.DirectoryExists("ActivityTracker"))
                        isoStore.CreateDirectory("ActivityTracker");

                    byte[] MyStream = e.Result;
                    string filename = "ActivityTracker/" + e.restorefilename;

                    if (isoStore.FileExists(filename))
                        isoStore.DeleteFile(filename);

                    if (!isoStore.FileExists(filename))
                    {
                        isostream = new IsolatedStorageFileStream(filename, FileMode.Create, isoStore);
                        int read = MyStream.Length;
                        isostream.Write(MyStream, 0, read);
                        isostream.Close();
                    }
                }
                if (restoreFileCount == restoreFileCompletedCount + restoreFileNotCompletedCount)
                {
                    string str = "";
                    if (restoreFileNotCompletedCount > 0)
                        str = "Some data not restored, please try again later";
                    else
                        str = "All data successfully restored";
                    MessageBox.Show(str);

                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in service1_RestoreUserDataCompleted Method In Settings file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                //  Exceptions.SaveOrSendExceptions(mess, ex.Data);
            }
        }
#endif
    }
}
